//
//  AttentionTopicVC.m
//  APP_login
//
//  Created by edz on 7/20/21.
//  Copyright © 2021 baicai. All rights reserved.
//
#import "AttentionTopicCell.h"
#import "AttentionTopicModel.h"
#import "AttentionTopicVC.h"
#import "Masonry.h"
#import "AFNetworking.h"
#import "MJRefresh.h"

@interface AttentionTopicVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *attentionTopicTableView;
@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, strong) MJRefreshNormalHeader *header;
@property (nonatomic, strong) MJRefreshBackNormalFooter *footer;
@property (nonatomic, assign) BOOL isPost;
@property (nonatomic, assign) BOOL isLastPage;
@property (nonatomic, assign) NSInteger tailID;
@end

@implementation AttentionTopicVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _modelArray = [NSMutableArray array];
    _isLastPage = NO;
    _isPost = YES;
    _tailID = 0;
    
    self.view.backgroundColor = [UIColor whiteColor];

    _attentionTopicTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_attentionTopicTableView];
    [_attentionTopicTableView mas_makeConstraints:^(MASConstraintMaker *make){
          make.top.left.right.bottom.mas_equalTo(0);
      }];
    _attentionTopicTableView.dataSource = self;
    _attentionTopicTableView.delegate = self;
    [_attentionTopicTableView registerClass:[AttentionTopicCell class] forCellReuseIdentifier:@"topicLable"];

    _header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh:)];
    _header.lastUpdatedTimeLabel.hidden = YES;
    _attentionTopicTableView.mj_header = _header;
    
    _footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh:)];
    _footer.automaticallyChangeAlpha = YES;
    _attentionTopicTableView.mj_footer = _footer;
    [self getData];
}

- (void) headerRefresh:(MJRefreshNormalHeader *) header{
    _tailID = 0;
    [self getData];
}

- (void) footerRefresh:(MJRefreshBackNormalFooter *) footer{
    if (!_isLastPage) {
        AttentionTopicModel *model = self.modelArray[self.modelArray.count-1];
        _tailID = model.topicID;
        [self getData];
    }else{
        [footer endRefreshingWithNoMoreData];
    }
}

-(void)getData{
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSDictionary *dict = @{@"userID" : @"80813", @"userKey" : @"GmRlVVlIgJnCjlBIhhsYd6Oh0wEXahOc", @"tailID" : @(self.tailID)};
    if (self.isPost) {
        _isPost = NO;
        [manger POST:@"http://118.31.222.92/topic/user-follow-list" parameters:dict progress:^(NSProgress *_Nonnull downloadProgress){
              
          }success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject){
              self.isLastPage = [[responseObject objectForKey:@"isEnd"] boolValue];
              NSMutableArray *array = [AttentionTopicModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"list"] error:nil];
              if (array) {
                  if (self.tailID == 0) {
                      [self.modelArray removeAllObjects];
                      self.modelArray = array;
                  }else{
                      [self.modelArray addObjectsFromArray:array];
                  }
                  [self.attentionTopicTableView reloadData];
              }
              if (!self.isLastPage && self.footer.state == MJRefreshStateNoMoreData) {
                  [self.footer resetNoMoreData];
              }
              [self endRefresh];
              self.isPost = YES;
          } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error){
              NSLog(@"出错了:%@", error);
              [self endRefresh];
              self.isPost = YES;
        }];
    }
}

- (void) endRefresh{
    if ( [self.header isRefreshing]) {
        [self.header endRefreshing];
    }
    if ([self.footer isRefreshing]) {
        if (!self.isLastPage) {
            [self.footer endRefreshing];
        }else{
            [self.footer endRefreshingWithNoMoreData];
        }
    }
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AttentionTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topicLable"];
    if (cell == nil) {
        cell = [[AttentionTopicCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"topicLable"];
    }
    cell.model = _modelArray[indexPath.row];
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
